//package com.paypalpay.controller.inside;
//
//import com.paypalpay.entity.common.DevicePackageCoverPicture;
//import com.paypalpay.entity.common.InternationalPayPackage;
//import com.paypalpay.entity.common.PartnerProductPackage;
//import com.paypalpay.pojo.Constant;
//import com.paypalpay.pojo.request.RequestBody.SyncModulePackagesRequestBody;
//import com.paypalpay.pojo.request.subRequest.SyncModulePackagesRequest;
//import com.paypalpay.service.InternationalPayPackageService;
//import com.paypalpay.service.PartnerService;
//import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
//import com.paypalpay.util.JaxbUtil;
//import com.paypalpay.util.wx.HttpClientTool;
//import com.paypalpay.xmlpojo.response.subResponse.SyncModulePackagesResponse;
//import com.paypalpay.xmlpojo.response.subResponse.common._Package;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpSession;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//@RestController
//public class SynchronousPackageController {
//
//    @Autowired
//    private HttpSession session;
//
//    @Autowired
//    public InternationalPayPackageService internationalPayPackageService;
//    @Autowired
//    PartnerService partnerService;
//
//    /**
//     * 获取设备套餐列表
//     *
//     * @param body 请求体  凝趣 测试渠道编码 P000512
//     * @return
//     */
//    @RequestMapping(value = "syncPackages", method = RequestMethod.GET)
//    @ResponseBody
//    public String synchronousPackage(SyncModulePackagesRequestBody body, @RequestParam(value = "partnerCode") String partnerCode) {
//        try {
//
//            String secret = partnerService.findSecretByPartnerCode(partnerCode);
//            // 2,生成当前时间戳
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String curtime = df.format(new Date());
//            // 请求参数拼接
//            SyncModulePackagesRequest request = new SyncModulePackagesRequest();
//            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
//            com.paypalpay.pojo.request.RequestHeader header = utils.getRequestHeader(Constant.cmd_syncModulePackages, partnerCode);
//            request.setHeader(header);
//            request.setBody(body);
//            String sign = utils.getSign(header, body, body.getClass(), secret);
//            request.setSign(sign);
//
//            // 将参数转换成XML格式
//            String sRequest = new JaxbUtil(SyncModulePackagesRequest.class).toXml(request, "utf-8");
//            System.out.println("请求值：" + sRequest);
//            // 发送post请求，并接收返回结果
//            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
//            System.out.println("返回结果：" + result);
//            // 将回传的xml转换成object数据
//            SyncModulePackagesResponse responseObj = new JaxbUtil(SyncModulePackagesResponse.class).fromXml(result, "utf-8");
//            // 将SyncModulePackagesResponse收到的package遍历出来
//            List<_Package> packageList = responseObj.getBody().get_package();
//
//            // 普通套餐
//            for (_Package p : packageList) {
//                String packageType = p.getPackageType();
//                String packageCode = p.getPackageCode();
//                System.out.println("获取到的产品编码：" + packageCode);
//                String packageName = p.getPackageName();
//                String packageDesc = p.getPackageDesc();
//                String coverCountryMcc = p.getCoverCountryMcc();
//                String coverCountrys = p.getCoverCountrys();
//                String appScope = p.getAppScope();
//                String priceType = p.getPriceType();
//                String packageFlow = p.getPackageFlow();
//                String buyType = p.getBuyType();
//                String minOrderPeriod = p.getMinOrderPeriod();
//                String maxOrderPeriod = p.getMaxOrderPeriod();
//                String mccs = "";
//                String continentName = "";
//                String coverCountryCode = "";
//                // 用packageCode前两位字母+后6位数字，位作为coverCountryCode值
//                coverCountryCode = packageCode.substring(0, 2) + packageCode.substring(packageCode.length() - 6, packageCode.length());
//                System.out.println("最终的coverCountryCode是" + coverCountryCode);
//                String partnerName = internationalPayPackageService.queryPartnerNameBypartnerCode(partnerCode);
//                System.out.println("取到的渠道名称：" + partnerName);
//                //将mcc用；截取，然后拿着数据去查询
//                String[] strs = coverCountryMcc.split(";");
//                for (int i = 0; i < strs.length; i++) {
//                    mccs = strs[i];
//                    continentName += internationalPayPackageService.queryContinent(mccs) + ";";
//                    //coverCountrys += internationalPayPackageService.queryCountry(mccs) + ";";
//                }
//                // 最后一个，去掉
//                if (!"".equals(continentName) || !"".equals(coverCountrys)) {
//                    continentName = continentName.substring(0, continentName.length() - 1);
//                    //coverCountrys = coverCountrys.substring(0, coverCountrys.length() - 1);
//                }
//                // 加油包
//                List<RefuelingPackage> refuelingPackageList = p.getRefuelingPackage();
//
//                if (refuelingPackageList == null || refuelingPackageList.size() < 0) {
//                    PartnerProductPackage product = new PartnerProductPackage();
//                    product.setPartnerCode(partnerCode);
//                    product.setPartnerName(partnerName);
//                    product.setPackageType(packageType);
//                    product.setPackageCode(packageCode);
//                    product.setPackageName(packageName);
//                    product.setPackageDesc(packageDesc);
//                    product.setCoverCountryMcc(coverCountryMcc);
//                    product.setCoverCountrys(coverCountrys);
//                    product.setContinentName(continentName);
//                    product.setAppScope(appScope);
//                    product.setPriceType(priceType);
//                    product.setPackageFlow(packageFlow);
//                    product.setBuyType(buyType);
//                    product.setMinOrderPeriod(minOrderPeriod);
//                    product.setMaxOrderPeriod(maxOrderPeriod);
//                    // 如果数据库中不存在当前套餐就入库套餐
//                    internationalPayPackageService.storagePackage(product);
//                    System.out.println("partner_product_t渠道套餐入库完成！");
//
//                    InternationalPayPackage payPackage = new InternationalPayPackage();
//                    payPackage.setPartnerCode(partnerCode);
//                    payPackage.setPartnerName(partnerName);
//                    payPackage.setPackageType(packageType);
//                    payPackage.setPackageCode(packageCode);
//                    payPackage.setPackageName(packageName);
//                    payPackage.setCoverCountryCode(coverCountryCode);
//                    payPackage.setStatus("0");
//                    payPackage.setBuyType(buyType);
//                    payPackage.setCoverCountryMcc(coverCountryMcc);
//                    payPackage.setCoverCountrys(coverCountrys);
//                    payPackage.setContinentName(continentName);
//                    payPackage.setAppScope(appScope);
//                    internationalPayPackageService.bak_storagePackage(payPackage);
//                    System.out.println("device_package_international_pay_t渠道套餐入库完成！");
//                    //图片信息入库
//                    int c = internationalPayPackageService.findPictureExistOrNot(packageCode, partnerCode);
//                    if (c <= 0) {
//                        DevicePackageCoverPicture picture = new DevicePackageCoverPicture();
//                        picture.setPartnerCode(partnerCode);
//                        picture.setPartnerName(partnerName);
//                        picture.setPackageCode(packageCode);
//                        picture.setCoverCountryCode(coverCountryCode);
//                        picture.setCoverCountrys(coverCountrys);
//                        picture.setCreateTime(curtime);
//                        internationalPayPackageService.storagePicture(picture);
//                        System.out.println("device_package_cover_picture_t入库完成！");
//                    }
//
//                } else {
//                    for (RefuelingPackage refueling : refuelingPackageList) {
//                        //套餐售卖信息，表明该套餐可以供哪些产品使用，售价是多少。
//                        String refPackageName = refueling.getRefPackageName();
//                        String refPackageCode = refueling.getRefPackageCode();
//                        String refPackageFlow = refueling.getRefPackageFlow();
//
//                        PartnerProductPackage product = new PartnerProductPackage();
//                        product.setPartnerCode(partnerCode);
//                        product.setPartnerName(partnerName);
//                        product.setPackageType(packageType);
//                        product.setPackageCode(packageCode);
//                        product.setPackageName(packageName);
//                        product.setPackageDesc(packageDesc);
//                        product.setCoverCountryMcc(coverCountryMcc);
//                        product.setCoverCountrys(coverCountrys);
//                        product.setContinentName(continentName);
//                        product.setAppScope(appScope);
//                        product.setPriceType(priceType);
//                        product.setPackageFlow(packageFlow);
//                        product.setBuyType(buyType);
//                        product.setMinOrderPeriod(minOrderPeriod);
//                        product.setMaxOrderPeriod(maxOrderPeriod);
//
//                        product.setRefPackageName(refPackageName);
//                        product.setRefPackageCode(refPackageCode);
//                        product.setRefPackageFlow(refPackageFlow);
//                        // 如果数据库中不存在当前套餐就入库套餐
//                        internationalPayPackageService.storageRefuelingPackage(product);
//                        System.out.println("partner_product_t渠道套餐入库完成！");
//
//                        InternationalPayPackage payPackage = new InternationalPayPackage();
//                        payPackage.setPartnerCode(partnerCode);
//                        payPackage.setPartnerName(partnerName);
//                        payPackage.setPackageType(packageType);
//                        payPackage.setPackageCode(packageCode);
//                        payPackage.setPackageName(packageName);
//                        payPackage.setCoverCountryCode(coverCountryCode);
//                        payPackage.setStatus("0");
//                        payPackage.setBuyType(buyType);
//                        payPackage.setCoverCountryMcc(coverCountryMcc);
//                        payPackage.setCoverCountrys(coverCountrys);
//                        payPackage.setContinentName(continentName);
//                        payPackage.setAppScope(appScope);
//                        internationalPayPackageService.bak_storagePackage(payPackage);
//                        System.out.println("device_package_international_pay_t渠道套餐入库完成！");
//
//                        //图片信息入库
//                        int c = internationalPayPackageService.findPictureExistOrNot(packageCode, partnerCode);
//                        if (c <= 0) {
//                            DevicePackageCoverPicture picture = new DevicePackageCoverPicture();
//                            picture.setPartnerCode(partnerCode);
//                            picture.setPartnerName(partnerName);
//                            picture.setPackageCode(packageCode);
//                            picture.setCoverCountryCode(coverCountryCode);
//                            picture.setCoverCountrys(coverCountrys);
//                            picture.setCreateTime(curtime);
//                            internationalPayPackageService.storagePicture(picture);
//                            System.out.println("device_package_cover_picture_t入库完成！");
//                        }
//
//                    }
//
//                }
//
//            }
//            // 返回错误结果的处理
//            String retCode = responseObj.getHeader().getResultCode();
//
//            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode))
//
//            {
//                System.out.println("返回结果：" + retCode);
//                return null;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}
